#include "stdafx.h"
#include "database.h"
#include <tchar.h>
#include <utility/utility.h>
#include <sqlite3/sqlite-src/sqlite3.h>
using namespace std;

namespace Zen
{
namespace database_helper
{
	string sql_string( const char* fmt, ... )
	{
		va_list ap;
		char *z;
#ifndef SQLITE_OMIT_AUTOINIT
		if( sqlite3_initialize() ) return "";
#endif
		va_start(ap, fmt);
		z = sqlite3_vmprintf(fmt, ap);
		va_end(ap);
		string ret(z);
		sqlite3_free(z);
		return ret;
	}
}

	void database::get_value( table_type& table, int row, int column, tstring& value )
	{
		if (0 == table.size() || row >= table.size() || column >= table[0].size()) return;
		value = table[row][column];
	}

	void database::get_value( table_type& table, int row, int column, int& value )
	{
		if (0 == table.size() || row >= table.size() || column >= table[0].size()) return;
		value = _ttoi(table[row][column].c_str());
	}

	void database::get_value( table_type& table, int row, int column, bool& value )
	{
		if (0 == table.size() || row >= table.size() || column >= table[0].size()) return;
		value = (table[row][column].at(0) != '\0');
	}

	void database::get_value( table_type& table, int row, int column, double& value )
	{
		if (0 == table.size() || row >= table.size() || column >= table[0].size()) return;
#ifdef _UNICODE
		value = _wtof(table[row][column].c_str());
#else
		value = atof(table[row][column].c_str());
#endif
	}


	string database::_get_coding_sql( const tstring& sql )
	{
#ifdef _UNICODE
		string s = utility::wstring_to_string(sql, CP_UTF8);
#else
		wstring ws = utility::string_to_wstring(sql, CP_ACP);
		string s = utility::wstring_to_string(ws, CP_UTF8);
#endif

		return s;
	}

	Zen::tstring database::get_error()
	{
		return error_;
	}

	bool database::has_table( const tstring& dbname, const tstring& table )
	{
		return true;
	}
}//end Zen

